package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.staservice.client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author dujiajia
 * @since 2020-11-07
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        // 添加之前先删除之前相同的日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        R r = ucenterClient.countRegister(day);
        Integer count = (Integer) r.getData().get("count");

        // 添加到统计分析表中

        StatisticsDaily sta = new StatisticsDaily();
        sta.setDateCalculated(day);
        // sta.setRegisterNum(count);
        sta.setRegisterNum(RandomUtils.nextInt(100,300));
        sta.setVideoViewNum(RandomUtils.nextInt(100,300));
        sta.setCourseNum(RandomUtils.nextInt(1,10));
        sta.setLoginNum(RandomUtils.nextInt(100,300));

        baseMapper.insert(sta);


    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);

        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);
        List<String> dateList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();

        for (int i = 0; i < staList.size(); i++) {
            StatisticsDaily staDaily = staList.get(i);
            // 日期的数组
            dateList.add(staDaily.getDateCalculated());
            //
            switch (type){
                case "login_num":
                    numList.add(staDaily.getLoginNum());
                    break;
                case "register_num":
                    numList.add(staDaily.getRegisterNum());
                    break;
                case "video_view_num":
                    numList.add(staDaily.getVideoViewNum());
                    break;
                case "course_num":
                    numList.add(staDaily.getCourseNum());
                    break;
                default:
                    break;
            }

        }

        Map<String,Object> map = new HashMap<>();
        map.put("dateList",dateList);
        map.put("numList",numList);
        return map;
    }
}
